package com.jincp.filepicker.adapter;

import android.view.View;

import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.utils.ToastUtil;
import com.jincp.filepicker.R;
import com.jincp.filepicker.ui.audio.AudioPickerManager;
import com.jincp.filepicker.bean.AudioFile;
import com.jincp.filepicker.util.MediaFileUtil;

/**
 * @ProjectName: filepicker.
 * @ClassName: ImagePickAdapter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/3/22 21:44.
 */
public class AudioPickAdapter extends BaseQuickAdapter<AudioFile, BaseViewHolder> {

    private int maxSelectedLimit = AudioPickerManager.getInstance().getMaxSelectedLimit();

    private int currentSelectedCount = 0;

    public AudioPickAdapter() {
        super(R.layout.item_audio_pick);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final AudioFile item) {
        holder.setText(R.id.tv_audio_title, item.getName());
        holder.setText(R.id.tv_duration, MediaFileUtil.getDurationString(item.getDuration()));

        holder.setSelected(R.id.iv_cbx, AudioPickerManager.getInstance().isContainSelected(item));
        holder.setVisible(R.id.iv_cbx, AudioPickerManager.getInstance().isMultiMode());
        holder.getView(R.id.iv_cbx).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doSelectedAudio(item, holder);
            }
        });
    }

    private void doSelectedAudio(AudioFile item, BaseViewHolder holder) {
        if (!AudioPickerManager.getInstance().isContainSelected(item) && isOverMaxSelectedLimit()) {
            ToastUtil.getInstance().show("已达到选择上限");
            return;
        }
        if (AudioPickerManager.getInstance().isContainSelected(item)) {
            AudioPickerManager.getInstance().removeSelectedFile(item);
            holder.setSelected(R.id.iv_cbx, false);
            currentSelectedCount--;
        } else {
            AudioPickerManager.getInstance().addSelectedFile(item);
            holder.setSelected(R.id.iv_cbx, true);
            currentSelectedCount++;
        }
    }

    public boolean isOverMaxSelectedLimit() {
        return currentSelectedCount >= maxSelectedLimit;
    }
}
